import { ref, Ref } from "vue";
import lodash from 'lodash';
const searchTableFilter = (tableColumns, searchTableParam?:any) =>{
    let dataListParam= ref([]);
    let tableFiltersList = ref([//selectAll是由对象属性 
        {labelName: '显示', list: 'isShowList', source: '资源设置'},{labelName: '系统', list: 'systemList', source: '资源设置'},
        {labelName: '状态', list: 'statusList', source: '部门设置、职位&权限、人员&权限'},{labelName: '级别', list: 'rankList', source: '职位&权限'},
        {labelName: '类型', list: 'typeList', source: '职位&权限'},  
        {labelName: '业绩岗', list: 'ifResultList', source: '职位&权限'},{labelName: '学区', list: 'areaList', source: '人员&权限', selectAll: true},    
        {labelName: '办公地点', list: 'officeList', source: '人员&权限', selectAll: true},{labelName: '员工类型', list: 'userTypeList', source: '人员&权限'},
    ]);

    return {
        dataListParam,
        /**
         * 合并行
         * @param {*} param0 
         * @returns 
         */
        spanMethod: ({ row, column, rowIndex, columnIndex })=> {
            let findArr = lodash.filter(tableColumns.value, item=> item.isShow);
            if(dataListParam.value && dataListParam.value.length){
                if (rowIndex === 0) {
                    if(columnIndex===1){
                        return {
                            rowspan: 1,
                            colspan: findArr.length+1,
                        };
                    }else{
                        return {
                            rowspan: 0,
                            colspan: 0
                        }
                    }
                }else{
                    return {
                        rowspan: 1,
                        colspan: 1
                    };
                }
            }else{
                return {
                    rowspan: 1,
                    colspan: 1
                };
            }
        },
        /**
         * 表格内容筛选条件 展示内容
         */
        getDataListParam: (newVal:any, labelName:any)=>{    
            let list = tableFiltersList.value.filter(item=> item.labelName == labelName);   
            // if(!list.length) return;
            let filterList = [];
            if(list.length){
                let item = list[0];
                filterList =  item.selectAll ?  (searchTableParam.value.selectAllList && searchTableParam.value.selectAllList[item['list']]) : searchTableParam.value[item['list']];
            }     
            if((!lodash.isArray(newVal) && (newVal || newVal===0)) || (lodash.isArray(newVal) && newVal.length)){
                let obj = {};
                if(labelName=='显示' || labelName=='系统' || labelName=='状态' || labelName == '级别' || labelName=='类型' || 
                labelName=='业绩岗' || labelName=='学区' || labelName=='办公地点' || labelName=='员工类型'){//多选
                    obj ={
                        labelName: labelName,
                        names: lodash.chain(filterList)
                            .filter(obj => newVal.includes(Number(obj.id)))
                            .map(obj => obj.name)
                            .value()
                    }
                }else{
                    obj ={
                        labelName: labelName,
                        names: newVal
                    }
                }
                let resultIndex = lodash.findIndex(dataListParam.value, item=> item.labelName==labelName);
                if(resultIndex>-1){
                    dataListParam.value[resultIndex] = obj;
                }else{
                    dataListParam.value.push(obj);
                }
            }else{
                let resultIndex = lodash.findIndex(dataListParam.value, item=> item.labelName==labelName);
                if(resultIndex>-1){
                    dataListParam.value.splice(resultIndex,1);
                }
            }     
        },
    }
};
export default searchTableFilter;